package com.liang.controller;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class ProcessController {
	@Autowired
	RepositoryService repositoryService;
	
	@RequestMapping("/process")
	public String index1() {
		return "process";
	}
	
	@ResponseBody
	@RequestMapping("/process/index.do")
	public Object index(@RequestParam(value = "pn",required = false,defaultValue = "1")int pn) {
		PageHelper.startPage(pn, 4);
		List<ProcessDefinition> pd = repositoryService.createProcessDefinitionQuery().list();
		
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		for (ProcessDefinition processDefinition : pd) {
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("name", processDefinition.getName());
			map.put("version", processDefinition.getVersion());
			map.put("key", processDefinition.getKey());
			map.put("deploymentId", processDefinition.getDeploymentId());
			map.put("resourceName", processDefinition.getResourceName());
			map.put("diagramResourceName", processDefinition.getDiagramResourceName());
			list.add(map);
		}
		
		PageInfo<Map<String,Object>> page=new PageInfo<Map<String,Object>>(list);
//		System.out.println(page.getList());
//		for (ProcessDefinition processDefinition : page.getList()) {
//			System.out.println(processDefinition);
//			System.out.println(processDefinition.getKey());
//			System.out.println(processDefinition.getVersion());
//			System.out.println(processDefinition.getName());
//		}
		System.out.println("==========="+page.getPageSize());
		return page;
	}
	@RequestMapping("/process/deploy")
	public String deploy(@RequestParam("files")CommonsMultipartFile file,HttpServletRequest request) throws IOException {
//		MultipartHttpServletRequest multipartHttpServletRequest=(MultipartHttpServletRequest)request;
//		MultipartFile file2 = multipartHttpServletRequest.getFile("file");
		System.out.println(file.getContentType());
		System.out.println();
		if(file.getFileItem().getName().contains(".bpmn")) {
			repositoryService.createDeployment().addInputStream(file.getOriginalFilename(), file.getInputStream()).deploy();
			System.out.println("部署成功了么？");
			return "process";
		}else {
			return "/error/404";
		}
	}
	
	@ResponseBody
	@RequestMapping("/process/delete")
	public String delete(@RequestParam("deploymentId")String id) {
		repositoryService.deleteDeployment(id, true);
		return id;
	}
	@ResponseBody
	@RequestMapping("/process/look")
	public void look(@RequestParam("deploymentId")String id,HttpServletResponse response) throws IOException {
		ProcessDefinition singleResult = repositoryService.createProcessDefinitionQuery().deploymentId(id).singleResult();
		InputStream inputStream = repositoryService.getResourceAsStream(id, singleResult.getResourceName());
//		InputStream inputStream = repositoryService.getResourceAsStream(id, singleResult.getDiagramResourceName());
		OutputStream outputStream = response.getOutputStream();
		System.out.println("=======================");
		IOUtils.copy(inputStream, outputStream);
	}
	@ResponseBody
	@RequestMapping("/process/look2")
	public String look2(@RequestParam("deploymentId")String id) {
		ProcessDefinition singleResult = repositoryService.createProcessDefinitionQuery().deploymentId(id).singleResult();
		String name = singleResult.getResourceName();
		System.out.println(name);
		String name2=name.split("\\.")[0]+".png";
		System.out.println(name2);
//		InputStream inputStream = repositoryService.getResourceAsStream(id, singleResult.getDiagramResourceName());
		System.out.println("++++++++++++++++++++");
		
		return name2;
	}
	@ResponseBody
	@RequestMapping("/process/go")
	public void go(@RequestParam("deploymentId")String id,HttpServletResponse response) throws IOException {
		ProcessDefinition singleResult = repositoryService.createProcessDefinitionQuery().deploymentId(id).singleResult();
		InputStream inputStream = repositoryService.getResourceAsStream(id, singleResult.getResourceName());
//		InputStream inputStream = repositoryService.getResourceAsStream(id, singleResult.getDiagramResourceName());
		OutputStream outputStream = response.getOutputStream();
		System.out.println("=======================");
		IOUtils.copy(inputStream, outputStream);
	}
}
